<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>全排列算法详解</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
        }
        .serif-font {
            font-family: 'Noto Serif SC', serif;
        }
        .gradient-text {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .code-container {
            background: #1e1e1e;
            border-radius: 12px;
            overflow: hidden;
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .code-header {
            background: #2d2d2d;
            padding: 12px 20px;
            display: flex;
            align-items: center;
            gap: 8px;
        }
        .code-dot {
            width: 12px;
            height: 12px;
            border-radius: 50%;
        }
        pre {
            margin: 0;
            padding: 20px;
            overflow-x: auto;
        }
        code {
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
            font-size: 14px;
            line-height: 1.6;
            color: #d4d4d4;
        }
        .keyword { color: #569cd6; }
        .type { color: #4ec9b0; }
        .function { color: #dcdcaa; }
        .string { color: #ce9178; }
        .comment { color: #6a9955; }
        .number { color: #b5cea8; }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-4px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            margin-top: -0.2rem;
            color: #667eea;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6">
        <div class="max-w-4xl mx-auto text-center">
            <h1 class="text-5xl md:text-6xl font-bold mb-6 serif-font">全排列算法</h1>
            <p class="text-xl md:text-2xl opacity-90 leading-relaxed">
                探索回溯算法的优雅之美，掌握生成所有可能排列的核心技巧
            </p>
            <div class="mt-8 flex justify-center gap-4">
                <span class="inline-flex items-center px-4 py-2 bg-white bg-opacity-20 rounded-full text-sm">
                    <i class="fas fa-clock mr-2"></i>时间复杂度 O(n!)
                </span>
                <span class="inline-flex items-center px-4 py-2 bg-white bg-opacity-20 rounded-full text-sm">
                    <i class="fas fa-memory mr-2"></i>空间复杂度 O(n)
                </span>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-6xl mx-auto px-6 py-12">
        <!-- Problem Description -->
        <section class="mb-16">
            <div class="bg-white rounded-2xl shadow-lg p-8 card-hover">
                <h2 class="text-3xl font-bold mb-6 flex items-center">
                    <i class="fas fa-puzzle-piece mr-3 text-purple-600"></i>
                    <span class="gradient-text">题目描述</span>
                </h2>
                <p class="text-lg leading-relaxed text-gray-700">
                    <span class="drop-cap serif-font">给</span>定一个不含重复数字的数组 nums，返回其所有可能的全排列。这是一道经典的回溯算法题目，要求我们找出数组中所有元素的不同排列组合。
                </p>
            </div>
        </section>

        <!-- Algorithm Visualization -->
        <section class="mb-16">
            <div class="bg-white rounded-2xl shadow-lg p-8">
                <h2 class="text-3xl font-bold mb-6 flex items-center">
                    <i class="fas fa-sitemap mr-3 text-purple-600"></i>
                    <span class="gradient-text">算法可视化</span>
                </h2>
                <div class="mermaid">
                    graph TD
                        A[开始: nums = 1,2,3] --> B{选择第一个位置}
                        B --> C[选择1]
                        B --> D[选择2]
                        B --> E[选择3]
                        
                        C --> F{剩余: 2,3}
                        F --> G[1,2,_]
                        F --> H[1,3,_]
                        
                        D --> I{剩余: 1,3}
                        I --> J[2,1,_]
                        I --> K[2,3,_]
                        
                        E --> L{剩余: 1,2}
                        L --> M[3,1,_]
                        L --> N[3,2,_]
                        
                        G --> O[1,2,3]
                        H --> P[1,3,2]
                        J --> Q[2,1,3]
                        K --> R[2,3,1]
                        M --> S[3,1,2]
                        N --> T[3,2,1]
                        
                        style A fill:#667eea,stroke:#fff,color:#fff
                        style O fill:#48bb78,stroke:#fff,color:#fff
                        style P fill:#48bb78,stroke:#fff,color:#fff
                        style Q fill:#48bb78,stroke:#fff,color:#fff
                        style R fill:#48bb78,stroke:#fff,color:#fff
                        style S fill:#48bb78,stroke:#fff,color:#fff
                        style T fill:#48bb78,stroke:#fff,color:#fff
                </div>
            </div>
        </section>

        <!-- Core Concepts -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 text-center">
                <span class="gradient-text">核心概念解析</span>
            </h2>
            <div class="grid md:grid-cols-3 gap-6">
                <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                    <div class="text-4xl text-purple-600 mb-4">
                        <i class="fas fa-undo-alt"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">回溯算法</h3>
                    <p class="text-gray-600">通过试探性地选择元素，当发现当前选择无法得到有效解时，退回上一步重新选择。</p>
                </div>
                <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                    <div class="text-4xl text-purple-600 mb-4">
                        <i class="fas fa-check-double"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">状态标记</h3>
                    <p class="text-gray-600">使用布尔数组记录每个元素的使用状态，确保每个元素在一个排列中只出现一次。</p>
                </div>
                <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                    <div class="text-4xl text-purple-600 mb-4">
                        <i class="fas fa-layer-group"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">递归深度</h3>
                    <p class="text-gray-600">递归深度等于数组长度，每层递归选择一个未使用的元素加入当前排列。</p>
                </div>
            </div>
        </section>

        <!-- Solution Code -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold mb-8 flex items-center">
                <i class="fas fa-code mr-3 text-purple-600"></i>
                <span class="gradient-text">完整解决方案</span>
            </h2>
            <div class="code-container">
                <div class="code-header">
                    <div class="code-dot bg-red-500"></div>
                    <div class="code-dot bg-yellow-500"></div>
                    <div class="code-dot bg-green-500"></div>
                    <span class="text-gray-400 ml-4">Permutation.java</span>
                </div>
                <pre><code><span class="keyword">public</span> <span class="type">List</span>&lt;<span class="type">List</span>&lt;<span class="type">Integer</span>&gt;&gt; <span class="function">permute</span>(<span class="keyword">int</span>[] nums) {
    <span class="comment">// 结果列表，存储所有可能的排列</span>
    <span class="type">List</span>&lt;<span class="type">List</span>&lt;<span class="type">Integer</span>&gt;&gt; result = <span class="keyword">new</span> <span class="type">ArrayList</span>&lt;&gt;();
    
    <span class="comment">// 当前正在构建的排列</span>
    <span class="type">List</span>&lt;<span class="type">Integer</span>&gt; current = <span class="keyword">new</span> <span class="type">ArrayList</span>&lt;&gt;();
    
    <span class="comment">// 标记数组中的哪些元素已经被使用</span>
    <span class="keyword">boolean</span>[] used = <span class="keyword">new</span> <span class="keyword">boolean</span>[nums.length];
    
    <span class="comment">// 调用回溯方法生成排列</span>
    <span class="function">backtrack</span>(nums, used, current, result);
    
    <span class="comment">// 返回所有排列结果</span>
    <span class="keyword">return</span> result;
}

<span class="comment">/**
 * 回溯方法，用于生成全排列
 * @param nums 原始数组
 * @param used 标记元素是否已使用
 * @param current 当前正在构建的排列
 * @param result 存储所有结果的列表
 */</span>
<span class="keyword">private</span> <span class="keyword">void</span> <span class="function">backtrack</span>(<span class="keyword">int</span>[] nums, <span class="keyword">boolean</span>[] used, 
                         <span class="type">List</span>&lt;<span class="type">Integer</span>&gt; current, <span class="type">List</span>&lt;<span class="type">List</span>&lt;<span class="type">Integer</span>&gt;&gt; result) {
    <span class="comment">// 如果当前排列长度等于原始数组长度，说明找到了一个完整的排列</span>
    <span class="keyword">if</span> (current.size() == nums.length) {
        <span class="comment">// 将当前排列加入结果集（需要创建一个新的列表以避免引用问题）</span>
        result.add(<span class="keyword">new</span> <span class="type">ArrayList</span>&lt;&gt;(current));
        <span class="keyword">return</span>;
    }
    
    <span class="comment">// 尝试将每个未使用的元素加入到当前排列</span>
    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; nums.length; i++) {
        <span class="comment">// 跳过已经使用过的元素</span>
        <span class="keyword">if</span> (used[i]) {
            <span class="keyword">continue</span>;
        }
        
        <span class="comment">// 标记当前元素为已使用</span>
        used[i] = <span class="keyword">true</span>;
        <span class="comment">// 将当前元素加入到排列中</span>
        current.add(nums[i]);
        
        <span class="comment">// 递归生成剩余元素的排列</span>
        <span class="function">backtrack</span>(nums, used, current, result);
        
        <span class="comment">// 回溯：移除最后添加的元素，将其标记为未使用</span>
        current.remove(current.size() - <span class="number">1</span>);
        used[i] = <span class="keyword">false</span>;
    }
}</code></pre>
            </div>
        </section>

        <!-- Algorithm Steps -->
        <section class="mb-16">
            <div class="bg-gradient-to-r from-purple-50 to-pink-50 rounded-2xl p-8">
                <h2 class="text-3xl font